package com.vq.controller;

import com.vq.common.R;
import com.vq.entity.Community;
import com.vq.entity.CommunityMember;
import com.vq.entity.User;
import com.vq.service.CommunityMemberService;
import com.vq.service.CommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/Community")
public class CommunityController {
    @Autowired
    HttpSession session;

    @Autowired
    CommunityService communityService;
    @Autowired
    CommunityMemberService communityMemberService;

    /**
     * 创建社区
     * @param communityName
     * @return
     */
    @PostMapping("/{communityName}")
    public R insert(@PathVariable("communityName") String communityName) {
        User user = (User) session.getAttribute("myUser");
        if (user==null) return new R(0);
        boolean bo = communityService.insert(user.getAccount(), communityName);
        log.info("创建社区："+communityName);
        return new R(bo?1:0);
    }

    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        User user = (User) session.getAttribute("myUser");
        if (user==null) return new R(0);
        boolean bo = communityService.deleteById(id, user.getAccount());
        return new R(bo?1:0);
    }

    @GetMapping
    public R<?> selectByIds(@RequestParam Long[] ids) {
        Community[] communities = communityService.selectByIds(ids);
        return new R<>(1, communities);
    }
    /**
     * 查询自己创建的社区
     * @return
     */
    @GetMapping("/1")
    public R selectByMe() {
        User user = (User) session.getAttribute("myUser");
        if (user==null) return new R(0);
        List<Community> communityList = communityService.selectByMe(user.getAccount());
        return new R(1, communityList);
    }
    /**
     * 查询自己关注的社区
     * @return
     */
    @GetMapping("/2")
    public R selectByMyFollower() {
        User user = (User) session.getAttribute("myUser");
        if (user==null) return new R(0);
        List<CommunityMember> communityMemberList = communityMemberService.selectByAccount(user.getAccount());
        List<Community> communityList = new ArrayList<>();
        for (CommunityMember communityMember : communityMemberList) {
            communityList.add(communityService.selectById(communityMember.getCommunityId()));
        }
        return new R(1, communityList);
    }
    /**
     * 搜素社区
     * @param name
     * @return
     */
    @GetMapping("/3")
    public R searchByName(@RequestParam("name") String name) {
        List<Community> communityList = communityService.searchByName(name);
        return new R(1, communityList);
    }
    /**
     * 查询对应名称的社区
     * @param name
     * @return
     */
    @GetMapping("/{name}")
    public R selectByName(@PathVariable("name") String name) {
        Community community = communityService.selectByName(name);
        return new R(community!=null?1:0, community);
    }
}
